/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { qqnews, ios, android, qq } from './ua';
import * as jsAPI from '@tencent/gh-qqnews-sdk/gh-qqnews-jsapi';
export const { getAppInfo } = jsAPI;
export const { getUserInfo } = jsAPI;
export const { setTitle } = jsAPI;
export const { closeWebview } = jsAPI;
export const { showActionMenu } = jsAPI;
export const { setShareArticleInfo } = jsAPI;
export const { setActionBtn } = jsAPI;
export const webCellReady = jsAPI.onWebCellReady;
export const { getVersion } = jsAPI;
// tslint:disable-next-line
let toastInstance = false;

export const toast = (text: string) => {
  if (toastInstance) {
    return;
  }
  toastInstance = true;
  const div = document.createElement('div');
  const { body } = document;
  div.classList.add('toast');
  div.innerHTML = text;
  body.appendChild(div);

  setTimeout(() => {
    (div.parentNode as HTMLElement).removeChild(div);
    toastInstance = false;
  }, 1500);
};

// 地理定位
export const getCurrentLocationInfo = (): Promise<any> => new Promise((resolve, reject) => {
  const callback = (data: any) => {
    resolve(data);
  };
  window.TencentNews.invoke('getCurrentLocationInfo', {
    onCallback: callback,
  });
});

export const tips = (text: string, type = 'success'): void => {
  if (qqnews) {
    const callback = () => {};
    window?.TencentNews?.invoke?.('showToast', {
      text,
      onCallback: callback,
    });
  } else {
    toast(text);
  }
};

// 跳转新的webview打开指定链接。type跳转的页面类型，1代表17文章类型（可分享），其他值会跳转6文章类型（不可分享）
export const openUrl = (url: string) => {
  const callback = () => {};
  window?.TencentNews?.invoke?.('openUrl', { url, onCallback: callback });
};

// h5大图分享
export const showBigImageShareMenu = (url: string, cb: Function) => {
  const callback = () => {};
  window?.TencentNews?.invoke?.('showBigImageShareMenu', {
    url,
    onCallback: cb || callback,
  });
};

export const reportToBoss = (params: any) => new Promise((resolve) => {
  window.reportToBossCallback = (ret: any) => {
    resolve(ret);
  };
  const par = {
    eventID: params.appEventId,
    params: params.params,
    onCallback: window.reportToBossCallback,
  };
  window?.TencentNews?.invoke?.('reportToBoss', par);
});

export const writeLog = (key: string, value: string) => {
  window.TencentNews.invoke('writeLog', { tag: key, msg: value });
};

// 获取android版本
export const getAndroidVersion = () => {
  const ua = navigator.userAgent.toLowerCase();
  let version = '0';
  if (ua.indexOf('android') > 0) {
    const reg = /android [\d._]+/gi;
    const vInfo = ua.match(reg);
    version = `${vInfo}`.replace(/[^0-9|_.]/gi, '').replace(/_/gi, '.'); // 得到版本号4.2.2
    [version] = version.split('.'); // 得到版本号第一位
  }
  return version;
};

export const login = () => new Promise<{ isLogin: boolean; userInfo?: {} }>((resolve) => {
  const callback = (res: { isLogin: boolean; userInfo?: {} }) => {
    // console.log('app:login:callback', res);
    resolve(res);
  };
  window?.TencentNews?.invoke?.('login', { onCallback: callback });
});
export const adjustWebViewInfo = (obj: Record<string, any>) => {
  if (window.TencentNews?.adjustWebViewInfo) {
    window.TencentNews.invoke('adjustWebViewInfo', obj);
  }
};
// 客户端cell注册相关事件监听
// resizeWindow：重置cell高度方法
// changeTheme：日夜间模式切换
interface WebCellManagerType {
  changeTheme: Function;
  resizeWindow?: Function;
  loginStatueChanged?: Function;
  channelDidRefreshData?: Function;
  channelDidAppear?: Function;
  channelDidDisappear?: Function;
  reload?: Function;
}
export const webCellManager = (obj: WebCellManagerType) => {
  window.webCellManager = {
    // 获取日夜间模式
    themeChanged: (res: any) => {
      // night default
      obj.changeTheme(res);
    },
    loginStatueChanged: (res: any) => {
      // loginStatueChanged
    },
    nativeDidFinishLoad: (res: any) => {
      // nativeDidFinishLoad
    },
    // 刷新
    channelDidRefreshData: (res: any) => {
      if (obj?.resizeWindow) obj.resizeWindow();
      if (ios) {
        webCellReady();
      }
    },
    channelDidAppear: (res: any) => {
      if (obj?.resizeWindow) obj.resizeWindow();
    },
    channelDidDisappear: (res: any) => {
      // channelDidDisappear
    },
    reload: (res: any) => {
      // reload
      if (obj?.resizeWindow) obj.resizeWindow();
    },
    onDetach: (res: any) => {
      // onDetach
    },
    onAttach: (res: any) => {
      // onAttach
    },
  };
};

export const webCellManagerPro = (obj: WebCellManagerType) => {
  window.webCellManager = {
    // 获取日夜间模式
    themeChanged: (res: any) => {
      // night default
      obj.changeTheme(res);
    },
    loginStatueChanged: (res: any) => {
      if (obj?.loginStatueChanged) obj.loginStatueChanged(res);
    },
    nativeDidFinishLoad: (res: any) => {
      // nativeDidFinishLoad
    },
    // 刷新
    channelDidRefreshData: (res: any) => {
      if (obj?.channelDidRefreshData) obj.channelDidRefreshData(res);
    },
    // 显示
    channelDidAppear: (res: any) => {
      if (obj?.channelDidAppear) obj.channelDidAppear(res);
    },
    // 消失
    channelDidDisappear: (res: any) => {
      if (obj?.channelDidDisappear) obj.channelDidDisappear(res);
    },
    reload: (res: any) => {
      if (obj?.reload) obj.reload(res);
    },
    onDetach: (res: any) => {
      // onDetach
    },
    onAttach: (res: any) => {
      // onAttach
    },
  };
};
